﻿//==============================================================================
//  File Name   :   SecureUrlHandler.cs
//
//  Copyright © 2007 by Do Hong Ngoc. All rights reserved.
//
//  Distributable under GPL code license.
//  See terms of license at www.bbquyen.com.
//
//==============================================================================

// <fileinformation>
//   <summary>
//     This file defines the SecureUrlHandler class.
//   </summary>
//   <author name="Do Hong Ngoc" mail="whiterose1611vn@yahoo.co.uk"/>
//   <date>3/31/2007 3:01:27 PM</date>
//   <remarks>
//     
//   </remarks>
// </fileinformation>

using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using BbqFramework.Security;

namespace Bbq.Gallery.UI
{
	/// <summary>
	/// Defines SecureUrlHandler class. It's encrypted path of an url in server.
	/// </summary>
	public class SecureUrlHandler : IHttpHandler
	{
		#region IHttpHandler Members

		/// <summary>
		/// Gets a value indicating whether another request can use the <see cref="T:System.Web.IHttpHandler"></see> instance.
		/// </summary>
		/// <value></value>
		/// <returns>true if the <see cref="T:System.Web.IHttpHandler"></see> instance is reusable; otherwise, false.</returns>
		public bool IsReusable
		{
			get { return true; }
		}

		/// <summary>
		/// Enables processing of HTTP Web requests by a custom HttpHandler that implements the <see cref="T:System.Web.IHttpHandler"></see> interface.
		/// </summary>
		/// <param name="context">An <see cref="T:System.Web.HttpContext"></see> object that provides references to the intrinsic server objects (for example, Request, Response, Session, and Server) used to service HTTP requests.</param>
		public void ProcessRequest(HttpContext context)
		{
			string encryptedUrl = context.Request.Url.Query.Substring(1);
			string url = SecureUrl.DecryptUrl(encryptedUrl);
            if (url.StartsWith("http:", StringComparison.OrdinalIgnoreCase) ||
                url.StartsWith("ftp:", StringComparison.OrdinalIgnoreCase))
            {
                context.Response.Clear();
                context.Response.Redirect(url, true);
            }
            else
            {
                context.Response.TransmitFile(url);
                context.Response.End();
            }
		}

		#endregion
	}
}
